package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excelData.ExcelSubjectData;
import com.atguigu.eduservice.entity.result.subject.OneSubject;
import com.atguigu.eduservice.entity.result.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 老王
 * @since 2020-05-19
 */
@Service
@Slf4j
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    //poi读取excel内容
    @Override
    public void importSubjectData(MultipartFile file,EduSubjectService subjectService) {
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();

            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e) {
            e.printStackTrace();
            throw new GuliException(20002,"添加课程分类失败");
        }
    }

    @Override
    public EduSubject getByTitle(String title,String parentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title)
                .eq("parent_id", parentId);
        return this.getOne(wrapper);
    }

    @Override
    public List<OneSubject> nestedList() {
        //1.先查出所有1级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0);
        List<EduSubject> oneList = this.list(wrapperOne);
        log.info("一级分类:"+oneList);

        //2.再查出所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0);//父亲id不等于0
        List<EduSubject> twoList = this.list(wrapperTwo);
        log.info("二级分类:"+twoList);

        ArrayList<OneSubject> list = new ArrayList<>();
        //3.循环把一级分类封装进结果list
        for (int i = 0; i < oneList.size(); i++) {
            OneSubject oneSubject = new OneSubject();
            //设置一级分类属性
            BeanUtils.copyProperties(oneList.get(i), oneSubject);

            //4.设置一级分类中的child，循环找到属于他的所有二级分类
            for (int j = 0; j < twoList.size(); j++) {
                EduSubject current=twoList.get(j);//当前二级分类
                if (oneSubject.getId().equals(current.getParentId())){ //如果当前父亲id等于 父亲分类id，则添加
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(current, twoSubject);
                    oneSubject.getChildren().add(twoSubject);//添加到 父亲属性中children
                }
            }
            list.add(oneSubject);//把一级分类添加进list
        }
        return list;

        //4.循环把二级分类封装进结果list

//        //先遍历每个一级分类
//        for (int i = 0; i < list.size(); i++) {
//            OneSubject parent=list.get(i);//父亲
//            for (int j = 0; j < twoList.size(); j++) {
//                EduSubject current=twoList.get(j);//当前二级分类
//                if (parent.getId().equals(current.getParentId())){ //如果当前父亲id等于 父亲分类id，则添加
//                    TwoSubject twoSubject = new TwoSubject();
//                    BeanUtils.copyProperties(current, twoSubject);
//                    parent.getChildren().add(twoSubject);//添加到 父亲属性中children
//                }
//            }
//        }

    }
}
